package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hsjk.szwj.framework.tenant.core.context.TenantContextHolder;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreatePlatformCourseReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminDeletePlatformCourseReqVO;
import com.hsjk.szwj.module.train.dal.dataobject.course.PlatformCourseMappingDO;
import com.hsjk.szwj.module.train.dal.mysql.course.IPlatformCourseMappingMapper;
import com.hsjk.szwj.module.train.service.IPlatformCourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/11/14 01:19
 **/
@Service
public class PlatformCourseServiceImpl implements IPlatformCourseService {
    @Resource
    private IPlatformCourseMappingMapper platformCourseMappingMapper;

    @Override
    public Boolean createPlatformCourse(AdminCreatePlatformCourseReqVO reqVO) {
        Long tenantId = TenantContextHolder.getTenantId();
        List<PlatformCourseMappingDO> platformCourseMappingDOS = new ArrayList<>();
        for (Long id : reqVO.getPlatformCourseIds()) {
            PlatformCourseMappingDO mappingDO = new PlatformCourseMappingDO();
            mappingDO.setPlatformCourseId(id);
            mappingDO.setTenantId(tenantId);
            platformCourseMappingDOS.add(mappingDO);
        }
        platformCourseMappingMapper.insertBatch(platformCourseMappingDOS, platformCourseMappingDOS.size());
        return Boolean.TRUE;
    }

    @Override
    public Boolean deletePlatformCourse(AdminDeletePlatformCourseReqVO reqVO) {
        List<Long> deleteIds = new ArrayList<>();
        for (Long id : reqVO.getPlatformCourseIds()) {
            PlatformCourseMappingDO mappingDO = platformCourseMappingMapper.selectOne(PlatformCourseMappingDO::getPlatformCourseId, id);
            if (ObjectUtil.isNotNull(mappingDO)) {
                deleteIds.add(mappingDO.getId());
            }
        }
        int resultCount = platformCourseMappingMapper.deleteBatchIds(deleteIds);
        return resultCount > 0 ? Boolean.TRUE : Boolean.FALSE;
    }
}
